package me.zhengjie.config.word.util;

import cn.hutool.core.util.ObjectUtil;
import com.deepoove.poi.data.ChartMultiSeriesRenderData;
import com.deepoove.poi.data.Charts;
import com.deepoove.poi.data.Texts;
import me.zhengjie.config.word.anno.Chart;
import me.zhengjie.config.word.anno.Html;
import me.zhengjie.config.word.anno.Text;
import me.zhengjie.modules.system.service.dto.ReportChartDto;
import me.zhengjie.utils.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class ChangDto2ChartViewUtil {

    public static Map<String,Object> convert(ReportChartDto reportChartDto) throws Exception{
        // 获取User类中所有的属性(getFields无法获得private属性)
        Map<String,Object> result = new HashMap<>();
        Field[] fields = ReportChartDto.class.getDeclaredFields();
        // 遍历所有属性
        for (Field field : fields) {
            field.setAccessible(true);
            if(ObjectUtil.isEmpty(field.getAnnotations())){
                continue;
            }

            String value = (String) field.get(reportChartDto);
            if(StringUtils.isEmpty(value)){
                continue;
            }
            // 如果属性上有此注解，则进行赋值操作
            if (field.isAnnotationPresent(Text.class)) {
                Text tx = field.getAnnotation(Text.class);
                result.put(field.getName(),Texts.of(value).color(tx.color()).create());
                continue;
            }

            if (field.isAnnotationPresent(Html.class)) {
                Html html = field.getAnnotation(Html.class);
                result.put(field.getName(),value);
                continue;
            }

            if (field.isAnnotationPresent(Chart.class)) {
                Chart chart = field.getAnnotation(Chart.class);
                String[] values = value.split(",");
                Double[] numbers = new Double[values.length];
                for(int i=0;i< values.length;i++){
                    numbers[i] = Double.parseDouble(values[i]);
                }
                ChartMultiSeriesRenderData chartMultiSeriesRenderData = Charts.ofMultiSeries("",chart.categories())
                        .addSeries("ATMR",numbers).create();
                result.put(chart.name(),chartMultiSeriesRenderData);
                continue;
            }
        }
        return result;
    }

}
